#include "utils.h"
#include <string>
#include <iostream>

using namespace std;

void reverse(char str[]){
	int length = strlen(str);
	for (int i = 0; i < length / 2; i++){
		char c = str[i];
		str[i] = str[length - i - 1];
		str[length - i - 1] = c;
	}

}
int isReverse(char str[]){
	int res = 1;
	int length = strlen(str);
	for (int i = 0; i < length / 2; i++){
		if (str[i] != str[length - i - 1]) {
			res = 0;
			break;
		}
	}
	return res;
}

int recursion(int n, int last){
	if (n == last)
		return last;
	else
		return recursion((n - 1), last) * n;
}

int getDay(int year, int month, int day){
	switch (month - 1)
	{
	case 12:
		day += 31;
	case 11:
		day += 30;
	case 10:
		day += 31;
	case 9:
		day += 30;
	case 8:
		day += 31;
	case 7:
		day += 31;
	case 6:
		day += 30;
	case 5:
		day += 31;
	case 4:
		day += 30;
	case 3:
		day += 31;
	case 2:
		(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? (day += 29) : (day += 28);
	case 1:
		day += 31;
	default:
		break;
	}
	return day;

}
void splitIp(char *str, int *res, char split){
	int length = strlen(str);
	int pos = 0, num = 0;
	for (int i = length - 1; i >= 0; i--){
		if (str[i] == split ){
			pos = 0;
			if (num >= 0 && num <= 255) {
				*res = 1;
			}
			else {
				*res = 0;
				return;
			}
			num = 0;
		}
		else {
			num += (str[i] - 48)*pow(10, pos);
			pos++;
		}
		
	}

	if (num >= 0 && num <= 255) {
		*res = 1;
	}
	else {
		*res = 0;
	}
}


void calculateTwoDateAvg(int d1, int d2, int *res){
	if (d1 + d2 >= 60){
		*res = 30 + (int)(d1 + d2 - 60) / 2;
	}
	else {
		*res = (int)(d1 + d2) / 2;
	}
}